function diff = J_fun( m, param)

    if param.K>0
        diff = J_n(m,param).*(m>=param.m_l).*(m<param.m_h) ...
            +(m>=param.m_h).*(m<param.m_e).*J_r(m,param) ...
            +(m>=param.m_e).*(param.c+param.K);
    else
        diff = J_n(m,param).*(m>=param.m_l).*(m<param.m_h) ...
            +(m>=param.m_h).*param.c;    
    end

end